//
//  JMBottomTabView.m
//  PhotoLock
//
//  Created by 赵俊明 on 2017/7/15.
//  Copyright © 2017年 赵俊明. All rights reserved.
//

#import "JMBottomTabView.h"

@implementation JMBottomTabView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = JMBaseColor;
    }
    return self;
}

- (void)setItems:(NSArray *)items
{
    _items = items;
    
    int i = 0;
    for (NSString *item in items) {
        
        UIButton *left = [UIButton buttonWithType:(UIButtonTypeSystem)];
        left.tag = i+baseTag;
        left.backgroundColor = [UIColor whiteColor];
//        left.tintColor = [UIColor whiteColor];
        // [left setImage:[UIImage imageNamed:@"add"] forState:(UIControlStateNormal)];
        [left setTitle:item forState:(UIControlStateNormal)];
        [left addTarget:self action:@selector(leftBtn:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:left];
        i ++;
    }
}

- (void)leftBtn:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(buttomTabSelection:)]) {
        
        [self.delegate buttomTabSelection:sender.tag-baseTag];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat margin = 2;
    NSInteger number = self.subviews.count;
    CGFloat width = (self.width-(number-1)*margin)/number;
    
    int i = 0;
    for (UIView *view in self.subviews) {
        
        view.frame = CGRectMake((width+margin)*i, 0, width, self.height);
        i ++;
    }
}

@end
